<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use \Mail;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/configs/goemail';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

        /*邮箱验证********************/
        $uid = $user->id;
        $email = $user->email;
        $username = $user->name;
        $activationcode = md5($user.time()); //得到一个邮箱验证的随机串
        $info = array('email' => $email, 'name'=>$username, 'uid'=>$uid,'activationcode'=>$activationcode);
        Mail::send('shared/activemail',$info,function($message) use($info){
          $message->to($info['email'],$info['name'])->subject('欢迎注册不去联盟帐号');
        });
        // session()->flash('success','请到个人邮箱中点击激活地址激活账户');
        session(['email'=>$email]);
        session(['activationcode'=>$activationcode]);
        /*邮箱验证********************/

        return $user;
    }

}
